package org.example.controller;

import org.example.entity.Car;
import org.example.service.CarService;
import org.example.service.CarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/cars")
public class CarController {
    @Autowired
    private CarService carService;

    @GetMapping
    public String getList(Model model){
        model.addAttribute("carList",carService.getList());
        return "car/list";
    }

    @GetMapping("/add")
    public String add(){
        return "car/add";
    }

    @PostMapping("/add")
    public String add(Car car){
        int count=carService.add(car);
        if(count==1){
            return "redirect:/cars";
        }
        return "car/add";
    }

    @GetMapping("/del/{carId}")
    public String del(@PathVariable int carId){
        carService.del(carId);
        return "redirect:/cars";
    }

    @GetMapping("/get/{carId}")
    public String update (@PathVariable int carId,Model model){
        model.addAttribute("getId",carService.get(carId));
        return "car/update";
    }

    @PostMapping("/update")
    public String update(Car car){
        int count=carService.update(car);
        if(count==1){
            return "redirect:/cars";
        }
        return "car/update";
    }


}
